﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MircoSoftTanslator.MircoSoftTranslatorService;

namespace MircoSoftTanslator
{
    public class Translator
    {
        private AdmAuthentication _admAuth;
        private LanguageServiceClient _client;

        public Translator(string clientID, string clientSercet, TimeSpan timeout)
        {
            this._admAuth = new AdmAuthentication(clientID, clientSercet);
            this._client = new LanguageServiceClient();
            this._client.InnerChannel.OperationTimeout = timeout;
        }

        public Translator(string clientID, string clientSercet)
            : this(clientID, clientSercet, new TimeSpan(0, 1, 0))
        {
        }

        public string Translate(string sourceText, string from, string to)
        {
            string headerValue = "Bearer " + this._admAuth.GetAccessToken().access_token;
            string result;
            try
            {
                result = this._client.Translate(headerValue, sourceText, from, to, "text/plain", "").ToLower();
            }
            catch
            {
                result = "";
            }
            return result;
        }

    }
}
